package com.proactive.webapp.controller;

import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.proactive.webapp.dao.MemberDAO;
import com.proactive.webapp.dao.UploadImageDAO;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.UploadImage;
import com.proactive.webapp.util.AwsUtil;
import com.proactive.webapp.util.CompressImage;
import com.proactive.webapp.util.DateConversion;
import com.proactive.webapp.util.FileUtil;

@Controller
public class UploadImageController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	@RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
	public @ResponseBody String uploadImage(HttpSession session, MultipartFile upload, HttpServletRequest request) throws Exception {
		Member loginMember= (Member)session.getAttribute("loginMember");
		String printString = "";
		if(loginMember != null){
			if(upload.getSize() > 0){
				MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");	
				UploadImageDAO uploadImageDAO = (UploadImageDAO) context.getBean("uploadImageDAO");			
				loginMember = memberDAO.get(loginMember);
				String path = session.getServletContext().getRealPath("resources/image/uploadImage");
				DateConversion dateConversion = new DateConversion();
				Date now = new Date();
				FileUtil fileUtil = new FileUtil(upload.getOriginalFilename());
				String newFileName = dateConversion.ConversionYMDHMS2(now) +"_"+ loginMember.getMemberNO() + "." + fileUtil.getFileType();
				File targetFile = new File(path, newFileName);
		        if(!targetFile.exists()){
		            targetFile.mkdirs();
		        }
		        try {
		        	upload.transferTo(targetFile);
		        } catch (Exception e) {
		            e.printStackTrace();
		        }
		        UploadImage uploadImage = new UploadImage();
		        uploadImage.setAccount(loginMember.getAccount());
		        uploadImage.setImage("resources/image/uploadImage/"+newFileName);
		        uploadImageDAO.insert(uploadImage);
		        if(!fileUtil.getFileType().equals("gif")){
		    		CompressImage compressImage = new CompressImage();
		        	compressImage.compress(path+"/", newFileName, 0, 0, false);
		        }
		        
		        AwsUtil awsUtil = new AwsUtil();
		        awsUtil.setPropertyPath(request.getServletContext().getRealPath("../AwsCredentials.properties"));
		        awsUtil.getInstance();
		        awsUtil.uploadFile("resources/image/uploadImage/", targetFile);//上傳
		        targetFile.delete();
		        
				printString += "<script type=\'text/javascript\'>";
				printString += "window.parent.CKEDITOR.tools.callFunction(1,'https://s3-ap-northeast-1.amazonaws.com/proactive/resources/image/uploadImage/"+newFileName+"','')";
				printString += "</script>";
			}
		}
		return printString;		
		
		/*
		Member loginMember= (Member)session.getAttribute("loginMember");
		String printString = "";
		if(loginMember != null){
			if(upload.getSize() > 0){
				MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");	
				UploadImageDAO uploadImageDAO = (UploadImageDAO) context.getBean("uploadImageDAO");			
				loginMember = memberDAO.get(loginMember);
				String path = session.getServletContext().getRealPath("resources/image/uploadImage");
				DateConversion dateConversion = new DateConversion();
				Date now = new Date();
				FileUtil fileUtil = new FileUtil(upload.getOriginalFilename());
				String newFileName = dateConversion.ConversionYMDHMS2(now) +"_"+ loginMember.getMemberNO() + "." + fileUtil.getFileType();
				File targetFile = new File(path, newFileName);
		        if(!targetFile.exists()){
		            targetFile.mkdirs();
		        }
		        try {
		        	upload.transferTo(targetFile);
		        } catch (Exception e) {
		            e.printStackTrace();
		        }
		        UploadImage uploadImage = new UploadImage();
		        uploadImage.setAccount(loginMember.getAccount());
		        uploadImage.setImage("resources/image/uploadImage/"+newFileName);
		        uploadImageDAO.insert(uploadImage);
		        if(!fileUtil.getFileType().equals("gif")){
		    		CompressImage compressImage = new CompressImage();
		        	compressImage.compress(path+"/", newFileName, 0, 0, false);
		        }
		        
		        
				printString += "<script type=\'text/javascript\'>";
				printString += "window.parent.CKEDITOR.tools.callFunction(1,'resources/image/uploadImage/"+newFileName+"','')";
				printString += "</script>";
			}
		}
		return printString;		
		*/
	}
}
